package com.example.jiaju.controller;

import com.example.jiaju.dataobject.User;
import com.example.jiaju.dto.UserNameDto;
import com.example.jiaju.enums.ResultEnum;
import com.example.jiaju.exception.SMException;
import com.example.jiaju.form.PhoneForm;
import com.example.jiaju.form.RegisterForm;
import com.example.jiaju.form.ResetPassForm;
import com.example.jiaju.form.UserNameForm;
import com.example.jiaju.until.AliyunMessageUtil;
import com.example.jiaju.until.ResultVOUntil;
import com.example.jiaju.vo.ResultVO;
import com.example.jiaju.service.impl.UserServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@CrossOrigin(
        origins = "*",
        maxAge = 3600,
        methods = {RequestMethod.GET, RequestMethod.POST}
)
public class UserController
{
    @Autowired
    private UserServiceImpl userService;

    //注册
    @PostMapping("/register")
    public ResultVO Register(@RequestBody @Valid RegisterForm registerForm, BindingResult bindingResult,HttpServletRequest request)
    {
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }

        //判断验证码
        HttpSession session=request.getSession();
        String code= (String) session.getAttribute("code");
        if(!registerForm.getVerificationCode().equals(code)){
            throw new SMException(ResultEnum.VERIFICATIONCODE_ERROR);
        }

        User user =new User();
        BeanUtils.copyProperties(registerForm, user);
        userService.register(user);

        return ResultVOUntil.success();
    }

    //修改密码
    @PostMapping(value = "/resetPassword")
    public ResultVO resetPassword(@RequestBody @Valid ResetPassForm resetPassForm,
                                  BindingResult bindingResult,
                                  HttpServletRequest request)
    {
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }

        //判断用户是否存在
        User user =userService.findByPhone(resetPassForm.getPhone());
        if(user ==null){
            throw new SMException(ResultEnum.USER_NOT_FOUND);
        }

        //判断验证码
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute("code");
        if(!resetPassForm.getVerificationCode().equals(code)){
            throw new SMException(ResultEnum.VERIFICATIONCODE_ERROR);
        }

        //修改用户
        user.setPass(resetPassForm.getNewPass());
        userService.save(user);
        return ResultVOUntil.success();
    }

    //修改用户名
    @PostMapping("/updateUserName")
    public ResultVO updateName(@RequestBody @Valid UserNameForm userNameForm,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        userService.updateName(userNameForm.getPhone(),userNameForm.getNewName());
        return ResultVOUntil.success();
    }

    //获取当前用户权限
//    @PostMapping(value = "/getUserRole")
//    public ResultVO getUserRole(String data)throws Exception{
//        return null;
//    }

    //获取当前用户信息
//    @PostMapping(value = "/getUserMsg")
//    public ResultVO getUserMsg(String data)throws Exception{
//        JsonNode jsonNode=mapper.readTree(data);
//        String phone=jsonNode.get("phone").asText();
//        User user=userService.findByPhone(phone);
//        if(user==null){
//            throw new SMException(ResultEnum.USER_NOT_FOUND);
//        }
//        return ResultVOUntil.success(user);
//    }

    //获取验证码
    @PostMapping(value = "/getVerificationCode")
    public ResultVO getVerificationCode(@RequestBody @Valid PhoneForm phoneForm,
                                        BindingResult bindingResult,HttpServletRequest request)throws Exception{
        if (bindingResult.hasErrors()){
            throw new SMException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }

        String code=""+AliyunMessageUtil.createRandomNum();
        System.out.println(code);
//        AliyunMessageUtil.sendMsg(phone,code);
        HttpSession session = request.getSession();
        session.setAttribute("code",code);

        return ResultVOUntil.success();
    }

}
